package controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

import data.Data;

public class PayCompleteServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		
		String invoice = request.getParameter("invoice");
		String payment_status = request.getParameter("payment_status");
		String mc_gross = request.getParameter("mc_gross");
		String payment_date  = request.getParameter("payment_date");
		String first_name = request.getParameter("first_name");
		String last_name  = request.getParameter("last_name");
		String payer_email = request.getParameter("payer_email");
		String payer_id = request.getParameter("payer_id");
		String address_name  = request.getParameter("address_name");
		String address_country = request.getParameter("address_country");
		String address_country_code  = request.getParameter("address_country_code");
		String address_zip = request.getParameter("address_zip");
		String address_state = request.getParameter("address_state");
		String address_city = request.getParameter("address_city");
		String address_street  = request.getParameter("address_street");
		
		if("Completed".equals(payment_status)&&invoice!=null)
		{
			String blog="";
			
			DBCollection coll = Data.data("users");
			
			DBObject dbObject = coll.findOne(new BasicDBObject("invoice",invoice));
			
			if(mc_gross!=null)
			{
				if(dbObject!=null)
				{
					int coin = 0;
					if(dbObject.get("coin")!=null)
					{
						coin = Integer.parseInt(dbObject.get("coin").toString());
					}
					
					coin += Integer.parseInt(String.valueOf(Integer.parseInt(mc_gross.replaceAll("[^0-9.]+", "").replaceAll("[.]{1}[0-9]+$", ""))/0.002).replaceAll("[.]{1}[0-9]+$", ""));
					
					dbObject.put("coin", coin);
					dbObject.put("status", "payment");
					
					coll.findAndModify(new BasicDBObject("invoice",invoice), dbObject);
					
					
					DBCollection collection = Data.data("urlCraw");
					DBCursor cursor = collection.find(new BasicDBObject("username",dbObject.get("username").toString()));
					while (cursor.hasNext()) {
						DBObject dbObject2 = (DBObject) cursor.next();
						dbObject2.put("active_craw", "yes");
						collection.save(dbObject2);
					}
					
				} else {
					DBCollection dbCollection = Data.data("logpay");
					BasicDBObject basicDBObject = new BasicDBObject();
					basicDBObject.put("invoice", invoice);
					basicDBObject.put("mc_gross", mc_gross);
					basicDBObject.put("date", new Date());
					dbCollection.insert(basicDBObject);
				}
			}
			
			DBCollection collection = Data.data("pay");
			BasicDBObject basicDBObject = new BasicDBObject();
			basicDBObject.put("invoice", invoice);
			basicDBObject.put("mc_gross", mc_gross);
			basicDBObject.put("blog", blog);
			basicDBObject.put("date", payment_date);
			basicDBObject.put("first_name", first_name);
			basicDBObject.put("last_name", last_name);
			basicDBObject.put("payer_email", payer_email);
			basicDBObject.put("payer_id", payer_id);
			basicDBObject.put("address_name", address_name);
			basicDBObject.put("address_country", address_country);
			basicDBObject.put("address_country_code", address_country_code);
			basicDBObject.put("address_zip", address_zip);
			basicDBObject.put("address_state", address_state);
			basicDBObject.put("address_city", address_city);
			basicDBObject.put("address_street", address_street);

			collection.insert(basicDBObject);
		}
	}
}
